﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;
using System.Data;
using System.Text;

public partial class Mrj_Shop : ExtBaseRpt
{
    mrjapp mrj = new mrjapp();
    string mid;
    protected void Page_Load(object sender, EventArgs e)
    {
        mid = Request.Params["mid"];
        if (!X.IsAjaxRequest)
        {
            BuildGridPanel(this.GridPanel1, mid, Session["UserID"].ToString());
            this.comShop.GetStore().DataSource = QueryShop("");
        }
    }
    protected void BtnQureyUser(object sender, DirectEventArgs e)
    {
        string str = "";
        string shop = StrSplitParam(comShop);
        shop = shop.Replace("''", "'");
        if (shop != "")
        {
            str += " and kmshopid in" + shop;
        }
        if (txtshop.Text != "")
        {
            str += " and mrjshopname like '%"+txtshop.Text+"%'";
        }
        string sql = @"select * from bas_mrj_shop where 1=1 " + str;
        this.Store1.DataSource = mydb.DM_GetDateSet(sql);
        this.Store1.DataBind();
        //QueryData(this.Store1, mid, sql);
    }
    protected void Cell_Click(object sender, DirectEventArgs e)
    {
        RowSelectionModel sm = this.GridPanel1.GetSelectionModel() as RowSelectionModel;
        this.hd_mrjshopid.Value = sm.SelectedRow.RecordID;
        //ShowBox(this.hd_mrjshopid.Value.ToString());
        //X.Msg.Alert("Submit", "You have submitted " + hf_user_id.Value + " nodes").Show();
    }
    protected void Shopselect(object sender, DirectEventArgs e)
    {
        //if (string.IsNullOrEmpty(this.hd_mrjshopid.Value.ToString()))
        //{
        //    ShowBox("操作错误！");
        //    return;
        //}
        string shopid = e.ExtraParams["shopid"].ToString();
        string shopname = e.ExtraParams["shopname"].ToString();
        string sql = " update Bas_mrj_Shop set kmshopid ='"+shopid+"',kmshopname = '"+shopname+"' where mrjshopid ='"+this.hd_mrjshopid.Value.ToString()+"'";
        if (mydb.DM_ExecuteNonQuery(sql) != -1)
        {
            this.Window1.Hide();
            BtnQureyUser(null, null);
        }
    }
    protected void BtnQureyShop(object sender, DirectEventArgs e)
    {
        OpenShop();
    }
    
    [DirectMethod(Namespace = "CompanyX")]
    public void OpenShop()
    {
        string sql = "select shopid,shopcode,shopname from bas_shop where 1=1"+(txtqshop.Text !=""?" and shopname like '%"+txtqshop.Text+"%'":"");
        this.Store4.DataSource = db.DM_GetDateSet(sql);
        this.Store4.DataBind();
    }
    protected void BtnQureyData(object sender, DirectEventArgs e)
    {
       //获取数据
        mrj.getshoplist();
        BtnQureyUser(null, null);
    }
    protected void BtnGetErpShop(object sender, DirectEventArgs e)
    {
        //获取数据
        string sql = "select *from Bas_mrj_Shop";
        StringBuilder sbr = new StringBuilder();
        DataSet ds = mydb.DM_GetDateSet(sql);
        if (ds != null)
        {
            if (ds.Tables[0].Rows.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    sbr.Append(@" update Bas_mrj_Shop set kmshopid = (select ShopID from V_Shop where shopname like '%" + dr["mrjshopname"] + @"%')
                                where mrjshopid = '" + dr["mrjshopid"] + "' and isnull(kmshopid,'')='' ");
                }
            }
        }
        if (mydb.DM_ExecuteNonQuery(sbr.ToString()) != -1)
        {
            ShowBox("更新成功！");
        }
        else
        {
            ShowBox("更新失败！");
        }
        BtnQureyUser(null, null);
    }

    protected void RefreshDataSet(object sender, StoreReadDataEventArgs e)
    {
        BtnQureyUser(null,null);
    }
    protected void Store1_Submit(object sender, StoreSubmitDataEventArgs e)
    {
        //导出EXCEL
        ExtGridToExcel(mid,"xls",e.Xml,this.Response,this.Title);
    }
}